package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.core.exception.ParameterException;
import com.demo.dao.PracticeProjectDao;
import com.demo.domain.PracticeProject;
import com.demo.service.PracticeProjectService;
import com.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PracticeProjectImpl extends ServiceImpl<PracticeProjectDao, PracticeProject> implements PracticeProjectService {
    @Autowired
    private PracticeProjectDao practiceProjectDao;

    @Autowired
    private StringUtils stringUtils;

    @Override
    public Page<PracticeProject> getProjectByKey(String type, String kw, int current, int size) throws ParameterException {
        if (current <= 0 || size <= 0) {
            throw new ParameterException("分页信息无效");
        }
        //条件+分页
        LambdaQueryWrapper<PracticeProject> lambdaQueryWrapper = new LambdaQueryWrapper<PracticeProject>();
        //根据不同的搜索条件来确定搜索的字段
        //type表示当前搜索的字段，根据type来确定数据库的搜索字段
        //kw表示搜索词，根据kw确定数据库的搜索内容
        if (type != null && kw != null) {
            if (type.equals("关键词")) {
//                    关键词搜索赛事名称
                lambdaQueryWrapper.like(PracticeProject::getPpName, kw);
            } else if (type.equals("年级")) {
                lambdaQueryWrapper.like(PracticeProject::getPpSuitableStudent, kw);
            } else if (type.equals("指导老师")) {
                lambdaQueryWrapper.like(PracticeProject::getPpGuidanceTeacher, kw);
            } else if (type.equals("类别")) {
                lambdaQueryWrapper.like(PracticeProject::getPpCategory, kw);
            }
        }
        lambdaQueryWrapper.eq(PracticeProject::getPpIsDelete, 0);
        Page page = new Page(current, size);
//            设置排序，按热度从高到低排序
        practiceProjectDao.selectPage(page, lambdaQueryWrapper.orderByDesc(PracticeProject::getPpPopularity));
        return page;
    }

    /**
     * 搜索赛事名
     *
     * @param kw
     * @return
     * @throws Exception
     */
    @Override
    public List<String> getProjectsByName(String kw, Long uid) throws Exception {
        if (uid == null || uid < 0l) {
            throw new ParameterException("UID为空或值非法");
        }
        //获取分词后的搜索词
        String newkw = stringUtils.splitWordsForSearch(kw);
        LambdaQueryWrapper<PracticeProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PracticeProject::getPpName, newkw);
        wrapper.eq(PracticeProject::getPpIsDelete, 0);
        //查询
        List<PracticeProject> projectList = practiceProjectDao.selectList(wrapper);
        if (projectList != null && projectList.size() > 0) {
            List<String> nameList = new ArrayList<>();
            for (PracticeProject project : projectList) {
                nameList.add(project.getPpName());
            }
            return nameList;
        }
        return null;
    }

    /**
     * 分页得所有赛事
     *
     * @param current
     * @param size
     * @return
     * @throws Exception
     */
    @Override
    public IPage getAllProjectsByPage(int current, int size) throws Exception {
        if (current <= 0 || size <= 0) {
            throw new ParameterException("分页信息无效");
        }
        LambdaQueryWrapper<PracticeProject> lambdaQueryWrapper = new LambdaQueryWrapper<PracticeProject>();
//            设置页面信息
        IPage page = new Page(current, size);
//            查询（热度降序）
        practiceProjectDao.selectPage(page, lambdaQueryWrapper.eq(PracticeProject::getPpIsDelete, 0).orderByDesc(PracticeProject::getPpPopularity));
        return page;
    }

    /**
     * 得到赛事详情
     *
     * @param name
     * @return
     * @throws Exception
     */
    @Override
    public PracticeProject getProjectDetailByName(String name) throws Exception {
        if (name == null || name.trim().length() < 1) {
            throw new ParameterException("搜索词为空");
        }
        LambdaQueryWrapper<PracticeProject> lambdaQueryWrapper = new LambdaQueryWrapper<PracticeProject>();
        lambdaQueryWrapper.eq(PracticeProject::getPpName, name.trim());
        lambdaQueryWrapper.eq(PracticeProject::getPpIsDelete, 0);
        PracticeProject pp = practiceProjectDao.selectOne(lambdaQueryWrapper);
        return pp;
    }

    @Override
    public LambdaQueryWrapper<PracticeProject> searchPracticeProject(String ppName, String ppSuitableStudent, String ppCategory) throws Exception {
        ppName = ppName == null ? "" : ppName.trim();
        ppSuitableStudent = ppSuitableStudent == null ? "" : ppSuitableStudent.trim();
        ppCategory = ppCategory == null ? "" : ppCategory.trim();
        LambdaQueryWrapper<PracticeProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!ppName.isEmpty(), PracticeProject::getPpName, ppName);
        wrapper.like(!ppSuitableStudent.isEmpty(), PracticeProject::getPpSuitableStudent, ppSuitableStudent);
        wrapper.like(!ppCategory.isEmpty(), PracticeProject::getPpCategory, ppCategory);
        wrapper.eq(PracticeProject::getPpIsDelete, 0);
        return wrapper;
    }
}
